package com.dmgis.qxfw.dataservice.businessservice.nyfw.controller;

import com.alibaba.fastjson.JSONObject;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.ny.*;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.admin_code;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.xt.users;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.yj.county_point;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.ResultObj;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.FileUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.dmgis_mongodb_srv.dataservice.service.Impl.MongoService1;
import com.dmgis.qxfw.dataservice.businessservice.nyfw.service.IFarmInfoService;
import com.dmgis.qxfw.dataservice.commonservice.common.DbConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.*;

/**
 * @ClassName FramInfoController
 * @Description TODO 农业展示后台
 * @Author sun
 * @Date 2021/7/13 16:04
 * @Version 1.0
 **/
@Controller
@RequestMapping("/nyfw")
public class FramInfoController {
    @Resource
    private IFarmInfoService farmInfoService;
    ProcessRequestController pc = new ProcessRequestController();


    MongoService1 ms = new MongoService1();

    private  final String operatingSystem;

    @Autowired
    public FramInfoController(String operatingSystem) {
        this.operatingSystem = operatingSystem;
    }


    /***
     * 根据用户名模糊查询用户信息
     * @return
     */
    @RequestMapping("/GetUserList")
    @ResponseBody
    public Object GetUserList(HttpServletRequest request) {
        String username = request.getParameter("username");
        List<AgriUser> userinfo = farmInfoService.getUserList(username);
        ResultObj resultObj = new ResultObj("查询成功", userinfo, true);
        return resultObj;
    }

    /***
     * 删除用户
     * @return
     */
    @RequestMapping("/DeleteUser")
    @ResponseBody
    public Object DeleteUser(HttpServletRequest request) {
        String id = request.getParameter("id");
        String msg = farmInfoService.deleteUser(id);
        ResultObj resultObj = new ResultObj("删除成功", msg, true);
        return resultObj;
    }

    /***
     * 添加用户信息
     * @return
     */
    @RequestMapping("/AddUser")
    @ResponseBody
    public Object AddUser(HttpServletRequest request) {
        String result = "";
        try {
            String username = request.getParameter("UserName");
            String password = request.getParameter("PassWord");
            if (StringUtil.IsNullOrEmpty(username) || StringUtil.IsNullOrEmpty(password))
                result = "传参错误！";
            AgriUser user = new AgriUser();
            user.setUsername(URLDecoder.decode(username, "UTF-8"));
            user.setPassword(URLDecoder.decode(password, "UTF-8"));
            user.setOwnercity(DbConfig.CITY);
            user.setOwnercounty(request.getParameter("QuXian"));
            user.setOwnertown(request.getParameter("XiangZheng"));
            //获取站点id
            Map<String, String> selectParam = new HashMap<String, String>();
            RequestDataBySelect selectRequest = new RequestDataBySelect();
            selectRequest.setParam("stationname = '" + request.getParameter("station") + "'");
            selectParam.put("method", "station_info");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = pc.processRequest(selectParam);
            List<StationInfo> stations = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<StationInfo>>() {
            });
            user.setStationid(stations.get(0).getStationid());
            user.setType(URLDecoder.decode(request.getParameter("UserType"), "UTF-8"));
            user.setRemarks(request.getParameter("Remark"));
            result = farmInfoService.addUser(user);
        } catch (Exception e) {
        }
        ResultObj resultObj = new ResultObj("查询成功", result, true);
        return resultObj;
    }


    /***
     * 更新用户信息
     * @return
     */
    @RequestMapping("/UpdateUser")
    @ResponseBody
    public Object UpdateUser(HttpServletRequest request) {
        String result = "";
        try {
            String id = request.getParameter("ID");
            String username = request.getParameter("UserName");
            String password = request.getParameter("PassWord");
            if (StringUtil.IsNullOrEmpty(id) || StringUtil.IsNullOrEmpty(username) || StringUtil.IsNullOrEmpty(password))
                result = "传参错误！";
            AgriUser user = new AgriUser();
            user.setUsername(URLDecoder.decode(username, "UTF-8"));
            user.setPassword(URLDecoder.decode(password, "UTF-8"));
            user.setOwnercity(DbConfig.CITY);
            user.setOwnercounty(request.getParameter("QuXian"));
            user.setOwnertown(request.getParameter("XiangZheng"));
            //获取站点id
            Map<String, String> selectParam = new HashMap<String, String>();
            RequestDataBySelect selectRequest = new RequestDataBySelect();
            selectRequest.setParam("stationname = '" + request.getParameter("station") + "'");
            selectParam.put("method", "station_info");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = pc.processRequest(selectParam);
            List<StationInfo> stations = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<StationInfo>>() {
            });
            user.setStationid(stations.get(0).getStationid());
            user.setType(URLDecoder.decode(request.getParameter("UserType"), "UTF-8"));
            user.setRemarks(request.getParameter("Remark"));
            result = farmInfoService.updateUser(id, user);

        } catch (Exception e) {
        }
        ResultObj resultObj = new ResultObj("查询成功", result, true);
        return resultObj;
    }





    /***
     * 删除问卷
     * @return
     */
    @RequestMapping("/DeleteWenJuan")
    @ResponseBody
    public Object DeleteWenJuan(HttpServletRequest request) {
        String ID = request.getParameter("ID");
        String meg = farmInfoService.deleteWenJuan(ID);
        ResultObj resultObj = new ResultObj("查询成功", meg, true);
        return resultObj;
    }
    /***
     * 删除链接
     * @return
     */
    @RequestMapping("/DeleteLink")
    @ResponseBody
    public Object DeleteLink(HttpServletRequest request) {
        String ID = request.getParameter("ID");
        String msg = farmInfoService.deleteLink(ID);
        ResultObj resultObj = new ResultObj("查询成功", msg, true);
        return resultObj;
    }

    /**
     * @Description: 网络底部栏  根据owner查询wangzhan并渲染
     * @Author: sunshine
     * @Date: 2021/8/1 11:45
     **/
    @RequestMapping("/BindShuju")
    @ResponseBody
    public Object BindShuju(HttpServletRequest request) {
        Object agrisrvList = farmInfoService.getCrossingGuardView();
        return agrisrvList;
    }

    /**
     * @Description: 新增网络底部栏
     * @Author: lw
     **/
    @RequestMapping("/addWangzhan")
    @ResponseBody
    public Object addWangzhan(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Wangzhan wangzhan = jsonObject.getObject("Wangzhan", Wangzhan.class);
                if (wangzhan == null)
                    return new ResultObj("新增失败!", "Wangzhan传递有误!", false);
                Object result = farmInfoService.insertWangzhan(wangzhan, user);
                return result;
            }
            return new ResultObj("新增失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }

    /**
     * @Description: 更新网络底部栏
     * @Author: lw
     **/
    @RequestMapping("/updateWangzhan")
    @ResponseBody
    public Object updateWangzhan(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Wangzhan wangzhan = jsonObject.getObject("Wangzhan", Wangzhan.class);
                String id = jsonObject.getString("id");
                if (wangzhan == null)
                    return new ResultObj("更新失败!", "Wangzhan传递有误!", false);
                Object result = farmInfoService.updateWangzhan(wangzhan,user,id);
                return result;
            }
            return new ResultObj("更新失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
    }


    /**
     * @Description: 添加小类别
     * @Author: lw
     **/
    @RequestMapping("/addSmallType")
    @ResponseBody
    public Object addSmallType(@RequestParam("params") String params,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                ArticleType articleType = jsonObject.getObject("articleType", ArticleType.class);
                if (articleType == null)
                    return new ResultObj("新增失败!", "articleType传递有误!", false);
                Object result = farmInfoService.insertSmallType(articleType,user);
                return result;
            }
            return new ResultObj("新增失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }

    /**
     * @Description: 更新文章类别
     * @Author: lw
     **/
    @RequestMapping("/updateType")
    @ResponseBody
    public Object updateType(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                ArticleType articleType = jsonObject.getObject("articleType", ArticleType.class);
                if (articleType == null)
                    return new ResultObj("更新失败!", "Wangzhan传递有误!", false);
                Object result = farmInfoService.updateType(articleType,user);
                return result;
            }
            return new ResultObj("更新失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
    }

    /**
     * @Description: 根据id删除新闻类别
     **/
    @RequestMapping("/deleteType")
    @ResponseBody
    public Object deleteType(@RequestParam("params") String params,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                String id = jsonObject.getString("id");
                Object type = farmInfoService.deleteType(id, user);
                return type;
            }
        } catch (Exception ex) {
            return new ResultObj("删除失败!",ex.getMessage(), false);
        }
        return new ResultObj("删除失败!",null, false);
    }

    /**
     * @Description: 查询链接列表
     **/
    @RequestMapping("/getLinks")
    @ResponseBody
    public Object getLinks(HttpServletRequest request) {
        List<Links> linksList = farmInfoService.getLinksList();
        try {
            for (Links links : linksList) {
                String serviceFilePath = "";
                // 下载
                String fileName = links.getLinkimg();
                String path = request.getContextPath();
                String basePath = DbConfig.IP + path
                        + "/Temp/Links/";
                String serviceUrl = basePath + fileName;
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                     urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Links\\";
                }else {
                   urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/Links/";
                }

                serviceFilePath = urlpath + fileName;// 服务器文件
                FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
                FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存
                // 如果目录中不存在文件，下载图片到temp目录
                File serviceFile = new File(serviceFilePath);// 服务器文件
                if (!serviceFile.exists()) {// 把文件上传到服务器
                    Map<String, Object> queryMap = new HashMap<String, Object>();
                    queryMap.put("id", links.getId());
                    queryMap.put("table_name", "links");
                    queryMap.put("advCode", DbConfig.advCode);
                    queryMap.put("collectionName", "image_file");
                    byte[] data = ms.selectData(queryMap);
                    if (data == null) {
                        serviceFilePath = "";
                    } else {
                        FileUtil.bytesToFile(data, serviceFilePath);
                    }
                }
                if (!serviceFilePath.equals("")) {
                    serviceFilePath = serviceUrl;
                }

                String imagepath = serviceFilePath;
                //添加图片到链接中
                links.setImage(imagepath);
            }
        } catch (Exception ex) {
            return new ResultObj("查询成功", ex.getMessage(), true);
        }
        return new ResultObj("查询成功", linksList, true);
    }


    /**
     * @Description: 添加文字链接
     * @Author: lw
     **/
    @RequestMapping(value ="/addTextLinks",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object addTextLinks(@RequestParam("params") String params,HttpServletRequest request) {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Links links = jsonObject.getObject("links", Links.class);
                if (links == null)
                    return new ResultObj("新增失败!", "links传递有误!", false);
                Links links1 = farmInfoService.insertLinks(links, user);
//                if (success == true) {
                    return new ResultObj("新增成功", "", true);
//                }
            }
        return new ResultObj("新增失败", "", true);
    }
    /**
     * @Description: 添加链接
     * @Author: lw
     **/
    @RequestMapping(value ="/addLinks",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object addLinks(@RequestParam("params") String params,@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Links links = jsonObject.getObject("links", Links.class);
                if (links == null)
                    return new ResultObj("新增失败!", "links传递有误!", false);
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                     urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Links\\";
                }else {
                urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/Links/";
                }

                FileUtil.mkDirs(urlpath);
                FileUtil.ClearTempFiles(urlpath, 100);
                String fileName = file.getOriginalFilename();// 文件名
                String fName = DateTime.now().toDateString() + fileName;
                links.setLinkimg(fName);
                String localPath = urlpath + fName;
                File localFile = new File(localPath);

                //新增pg库
                int returnTag = -1;
                if (localFile.exists()) {
                    returnTag = -1;
                }
                Links link = farmInfoService.insertLinks(links,user);
                //新增mg库
                if (link != null) {
                    byte[] bytes = file.getBytes();
                    String bytesString = new String(bytes, "ISO-8859-1");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", link.getId());
                    map.put("table_name", "links");
                    map.put("collectionName", "image_file");
                    map.put("advCode", DbConfig.advCode);
                    map.put("data", bytesString.getBytes("ISO-8859-1"));
                    returnTag = ms.insertData(map);
                    file.transferTo(localFile);
                    if (returnTag != -1) {
                        return new ResultObj("新增成功", returnTag, true);
                    }
                }
            }
            return new ResultObj("新增失败!", "", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }
    /**
     * 根据id修改友情链接 没有传图片
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/updateLks")
    @ResponseBody
    public Object updateLks(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Links links = jsonObject.getObject("links", Links.class);
                Links link = farmInfoService.updateLinks(links, user);
                return new ResultObj("更新成功!", link, true);
                }
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
        return new ResultObj("更新失败!", "", false);
    }
    /**
     * 根据id修改友情链接
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/updateLinks")
    @ResponseBody
    public Object updateLinks(@RequestParam("params") String params,@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Links links = jsonObject.getObject("links", Links.class);
                //更新pg和mg
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                   urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Links\\";
                }else {
                 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/Links/";
                }

                FileUtil.mkDirs(urlpath);
                FileUtil.ClearTempFiles(urlpath, 100);
                String fileName = file.getOriginalFilename();// 文件名
                String fName = DateTime.now().toDateString() + fileName;
                String localPath = urlpath + fName;
                links.setLinkimg(fName);
                File localFile = new File(localPath);
                if (links == null)
                    return new ResultObj("更新失败!", "links传递有误!", false);
                //更新pg库
                Links link = farmInfoService.updateLinks(links, user);
                int returnTag = -1;
                if (localFile.exists()) {
                    returnTag = -1;
                }
                //更新mg库
                if (link != null) {
                    byte[] bytes = file.getBytes();
                    String bytesString = new String(bytes, "ISO-8859-1");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", link.getId());
                    map.put("table_name", "links");
                    map.put("collectionName", "image_file");
                    map.put("advCode", DbConfig.advCode);
                    Map<String, Object> mapdata = new HashMap<String, Object>();
                    mapdata.put("data", bytesString.getBytes("ISO-8859-1"));
                    returnTag = (int) ms.updateData(map, mapdata);
                    file.transferTo(localFile);
                    if (returnTag != -1) {
                        return new ResultObj("更新成功", returnTag, true);
                    }
                }
            }
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
        return new ResultObj("更新失败!", "", false);
    }

    /**
     * @Description: 根据id删除友情链接
     **/
    @RequestMapping("/deleteLinks")
    @ResponseBody
    public Object deleteLinks(@RequestParam("params") String params,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                String id = jsonObject.getString("id");
                users user = jsonObject.getObject("user", users.class);
                //删除pg
                Object links = farmInfoService.deleteLinks(id, user);
                //删除mg
                if (links != null) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", id);
                    map.put("table_name", "links");
                    map.put("collectionName", "image_file");
                    map.put("advCode", DbConfig.advCode);
                    int returnTag = (int) ms.deleteData(map);
                    if (returnTag != -1) {
                        return new ResultObj("删除成功", returnTag, true);
                    }
                }
            }
        } catch (Exception e) {
            return new ResultObj("删除失败", "", true);
        }
        return new ResultObj("删除失败", "", true);
    }

    /**
     * @Description: 查询站点
     **/
    @RequestMapping("/getStations")
    @ResponseBody
    public Object getStations(HttpServletRequest request) {
        Object stations = farmInfoService.getStations();
        ResultObj resultObj = new ResultObj("查询成功", stations, true);
        return resultObj;
    }

    /**
     * @Description: 查询默认站点
     **/
    @RequestMapping("/getDefultStation")
    @ResponseBody
    public Object getDefultStation(HttpServletRequest request) {
        Object defaultStations = farmInfoService.getDefultStation();
        ResultObj resultObj = new ResultObj("查询成功", defaultStations, true);
        return resultObj;
    }

    /**
     * 更新默认站点
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/updateDefultStation")
    @ResponseBody
    public Object updateDefultStation(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                DefaultStation defaultStation = jsonObject.getObject("defaultstation", DefaultStation.class);
                String id = jsonObject.getString("id");
                if (defaultStation == null)
                    return new ResultObj("更新失败!", "defaultStation传递有误!", false);
                Object result = farmInfoService.updateDefultStation(defaultStation,user);
                return result;
            }
            return new ResultObj("更新失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
    }
    /**
     * @Description: 新增默认站点
     * @Author: lw
     **/
    @RequestMapping("/insertDefultStation")
    @ResponseBody
    public Object insertDefultStation(@RequestParam("params") String params,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                DefaultStation defaultStation = jsonObject.getObject("defaultstation", DefaultStation.class);
                if (defaultStation == null)
                    return new ResultObj("新增失败!", "defultstation传递有误!", false);
                Object result = farmInfoService.insertDefultStation(defaultStation,user);
                return result;
            }
            return new ResultObj("新增失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }

    /**
     * @Description: 查询所有用户名
     **/
    @RequestMapping("/getAllUsers")
    @ResponseBody
    public Object getAllUsers(HttpServletRequest request) {
        Object allUsers = farmInfoService.getAllUsers();
        ResultObj resultObj = new ResultObj("查询成功", allUsers, true);
        return resultObj;
    }

    /**
     * @Description: 查询日志
     **/
    @RequestMapping("/getLogs")
    @ResponseBody
    public Object getLogs(HttpServletRequest request) throws ParseException {
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String username = request.getParameter("username");
        String thing = request.getParameter("thing");
        Object logs = farmInfoService.getLogs(startDate,endDate,username,thing);
        return logs;
    }

    /**
     * @Description: 关键字搜索
     **/
    @RequestMapping("/getVideo")
    @ResponseBody
    public Object getVideo(HttpServletRequest request) throws ParseException {
        String title = request.getParameter("title");
        //查询pg表 模糊查询
        List<Video> videos = farmInfoService.getVideo(title);
        try {
            for (Video vdeo : videos) {
                String serviceFilePath = "";
                // 下载
                String fileName = vdeo.getVideopath();
                String path = request.getContextPath();
                String basePath = DbConfig.IP + path
                        + "/Temp/Video/";
                String serviceUrl = basePath + fileName;
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                   urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Video\\";
                }else {
             urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/Video/";
                }

                serviceFilePath = urlpath + fileName;// 服务器文件
                FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
                FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存
                // 如果目录中不存在文件，下载雷达文件到temp目录
                File serviceFile = new File(serviceFilePath);// 服务器文件
                if (!serviceFile.exists()) {// 把文件上传到服务器
                    Map<String, Object> queryMap = new HashMap<String, Object>();
                    queryMap.put("id", vdeo.getId());
                    queryMap.put("table_name", "video");
                    queryMap.put("advCode", DbConfig.advCode);
                    queryMap.put("collectionName", "video_file");
                    byte[] data = ms.selectData(queryMap);
                    if (data == null) {
                        serviceFilePath = "";
                    } else {
                        FileUtil.bytesToFile(data, serviceFilePath);
                    }
                }
                if (!serviceFilePath.equals("")) {
                    serviceFilePath = serviceUrl;
                }

                String imagepath = serviceFilePath;
                //添加视频路径
                vdeo.setVideo(imagepath);
            }
        } catch (Exception ex) {
            return new ResultObj("查询成功", ex.getMessage(), true);
        }
        return new ResultObj("查询成功", videos, true);
    }


    /**
     * @Description: 视频添加
     **/
    @RequestMapping("/addVideo")
    @ResponseBody
    public Object addVideo(@RequestParam("params") String params ,@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Video video = jsonObject.getObject("video", Video.class);
                if (video == null)
                    return new ResultObj("新增失败!", "video传递有误!", false);
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                     urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Video\\";
                }else {
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/Video/";
                }

                FileUtil.mkDirs(urlpath);
                FileUtil.ClearTempFiles(urlpath, 100);
                String fileName = file.getOriginalFilename();// 文件名
                String fName = DateTime.now().toDateString() + fileName;
                String localPath = urlpath + fName;
                video.setVideopath(fName);
                video.setPublisher(user.getUsername());
                video.setDatamodifydate(DateTime.now().toDate());
                video.setFinalmodifydate(DateTime.now().toDate());
                video.setVideoname("测试");
                video.setBigtype("视频信息库");
                video.setSmalltype("视频信息");
                video.setOwner(DbConfig.CITY);
                File localFile = new File(localPath);
                //新增pg库
                Video vdeo = farmInfoService.addVideo(video,user);
                int returnTag = -1;
                if (localFile.exists()) {
                    returnTag = -1;
                }
                //新增mg库
                if (vdeo != null) {
                    byte[] bytes = file.getBytes();
                    String bytesString = new String(bytes, "ISO-8859-1");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", vdeo.getId());
                    map.put("table_name", "video");
                    map.put("collectionName", "video_file");
                    map.put("advCode", DbConfig.advCode);
                    map.put("data", bytesString.getBytes("ISO-8859-1"));
                    returnTag = ms.insertData(map);
                    file.transferTo(localFile);
                    if (returnTag != -1) {
                        return new ResultObj("新增成功", returnTag, true);
                    }
                }
            }
            return new ResultObj("新增失败!", "", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }
    /**
     * 根据id修改视频 没有修改视频
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/updateVd")
    @ResponseBody
    public Object updateVd(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Video video = jsonObject.getObject("video", Video.class);
                video.setPublisher(user.getUsername());
                video.setFinalmodifydate(DateTime.now().toDate());
                Video vdeo = farmInfoService.updateVideo(video,user);
                return new ResultObj("更新成功!", vdeo, true);
            }
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
        return new ResultObj("更新失败!", "", false);
    }
    /**
     * 根据id修改视频
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/updateVideo")
    @ResponseBody
    public Object updateVideo(@RequestParam("params") String params, @RequestParam("file") MultipartFile file,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Video video = jsonObject.getObject("video", Video.class);
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Video\\";
                }else {
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/Video/";
                }
                FileUtil.mkDirs(urlpath);
                FileUtil.ClearTempFiles(urlpath, 100);
                String fileName = file.getOriginalFilename();// 文件名
                String fName = DateTime.now().toDateString() + fileName;
                String localPath = urlpath + fName;
                video.setPublisher(user.getUsername());
                video.setFinalmodifydate(DateTime.now().toDate());
                video.setVideopath(fName);
                File localFile = new File(localPath);
                if (video == null)
                    return new ResultObj("更新失败!", "video传递有误!", false);
                //修改pg库
                Video vdeo = farmInfoService.updateVideo(video,user);
                int returnTag = -1;
                if (localFile.exists()) {
                    returnTag = -1;
                }
                //修改mg库
                if (vdeo != null) {
                    byte[] bytes = file.getBytes();
                    String bytesString = new String(bytes, "ISO-8859-1");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", vdeo.getId());
                    map.put("table_name", "video");
                    map.put("collectionName", "video_file");
                    map.put("advCode", DbConfig.advCode);
                    Map<String, Object> mapdata = new HashMap<String, Object>();
                    mapdata.put("data", bytesString.getBytes("ISO-8859-1"));
                    returnTag = (int) ms.updateData(map, mapdata);
                    file.transferTo(localFile);
                    if (returnTag != -1) {
                        return new ResultObj("更新成功", returnTag, true);
                    }
                }
            }
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
        return new ResultObj("更新失败!", "", false);
    }

    /**
     * 删除视频
     * @param request
     * @return
     */
    @RequestMapping("/deleteVideo")
    @ResponseBody
    public Object deleteVideo(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                String id = jsonObject.getString("id");
                users user = jsonObject.getObject("user", users.class);
                //删除pg
                Object video = farmInfoService.deleteVideo(id, user);
                //删除mg
                if (video != null) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", id);
                    map.put("table_name", "video");
                    map.put("collectionName", "video_file");
                    map.put("advCode", DbConfig.advCode);
                    int returnTag = (int) ms.deleteData(map);
                    if (returnTag != -1) {
                        return new ResultObj("删除成功", returnTag, true);
                    }
                }
            }
        } catch (Exception e) {
            return new ResultObj("删除失败", "", false);
        }
        return new ResultObj("删除失败", "", true);
    }

    /**
     * @Description: 农气信息查询
     **/
    @RequestMapping("/getArcticlelist")
    @ResponseBody
    public Object getArcticlelist(HttpServletRequest request) throws ParseException {
        String title = request.getParameter("title");
        String bigtype = request.getParameter("bigtype");

        //查询pg表 模糊查询
        List<Addwendang> arcticlelist = farmInfoService.getArcticlelist(title, bigtype);
        try {
            for (Addwendang wendang : arcticlelist) {
                String serviceFilePath = "";
                // 下载
                String fileName = wendang.getPicpath();
                String path = request.getContextPath();
                String basePath = DbConfig.IP + path
                        + "/Temp/ArcticleList/";
                String serviceUrl = basePath + fileName;
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\ArcticleList\\";
                }else {
                urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/ArcticleList/";
                }

                serviceFilePath = urlpath + fileName;// 服务器文件
                FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
                FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存
                // 如果目录中不存在文件，下载雷达文件到temp目录
                File serviceFile = new File(serviceFilePath);// 服务器文件
                if (!serviceFile.exists()) {// 把文件上传到服务器
                    Map<String, Object> queryMap = new HashMap<String, Object>();
                    queryMap.put("id", wendang.getId());
                    queryMap.put("table_name", "addwendang");
                    queryMap.put("collectionName", "image_file");
                    queryMap.put("advCode", DbConfig.advCode);
                    byte[] data = ms.selectData(queryMap);
                    if (data == null) {
                        serviceFilePath = "";
                    } else {
                        FileUtil.bytesToFile(data, serviceFilePath);
                    }
                }
                if (!serviceFilePath.equals("")) {
                    serviceFilePath = serviceUrl;
                }

                String imagepath = serviceFilePath;
                //添加视频路径
                wendang.setPicpath(imagepath);
            }
        } catch (Exception ex) {
            return new ResultObj("查询成功", ex.getMessage(), true);
        }
        return new ResultObj("查询成功", arcticlelist, true);
    }

    /**
     * @Description: 新增农气信息
     **/
    @RequestMapping("/addArcticlelist")
    @ResponseBody
    public Object addArcticlelist(@RequestParam("params") String params ,@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Addwendang wendang = jsonObject.getObject("addwendang", Addwendang.class);
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\ArcticleList\\";
                }else {
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/ArcticleList/";
                }
                FileUtil.mkDirs(urlpath);
                FileUtil.ClearTempFiles(urlpath, 100);
                String fileName = file.getOriginalFilename();// 文件名
                String fName = DateTime.now().toDateString() + fileName;
                String localPath = urlpath + fName;
                wendang.setPublisher(user.getUsername());
                wendang.setDatamodifydate(DateTime.now().toDate());
                wendang.setPicpath(fName);
                wendang.setOwner(DbConfig.CITY);
                File localFile = new File(localPath);
                if (wendang == null)
                    return new ResultObj("新增失败!", "wendang传递有误!", false);
                //新增pg库
                Addwendang wdang = farmInfoService.addArcticlelist(wendang, user);
                int returnTag = -1;
                if (localFile.exists()) {
                    returnTag = -1;
                }
                //新增mg库
                if (wdang != null) {
                    byte[] bytes = file.getBytes();
                    String bytesString = new String(bytes, "ISO-8859-1");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", wdang.getId());
                    map.put("table_name", "addwendang");
                    map.put("collectionName", "image_file");
                    map.put("advCode", DbConfig.advCode);
                    map.put("data", bytesString.getBytes("ISO-8859-1"));
                    returnTag = ms.insertData(map);
                    file.transferTo(localFile);
                    if (returnTag != -1) {
                        return new ResultObj("新增成功", returnTag, true);
                    }
                }
            }
            return new ResultObj("新增失败!", "", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }
    /**
     * @Description: 新增农气信息不添加图片
     **/
    @RequestMapping("/addArcticleNoImage")
    @ResponseBody
    public Object addArcticleNoImage(@RequestParam("params") String params ,HttpServletRequest request) {
        if (!StringUtil.IsNullOrEmpty(params)) {
            JSONObject jsonObject = JSONObject.parseObject(params);
            users user = jsonObject.getObject("user", users.class);
            Addwendang wendang = jsonObject.getObject("addwendang", Addwendang.class);
            if (wendang == null)
                return new ResultObj("新增失败!", "wendang传递有误!", false);
            wendang.setPublisher(user.getUsername());
            wendang.setDatamodifydate(DateTime.now().toDate());
            wendang.setOwner(DbConfig.CITY);
            wendang.setPicpath(DateTime.now().toDateString());
            //新增pg库
            Addwendang wdang = farmInfoService.addArcticlelist(wendang, user);
            if (wdang == null) {
                return new ResultObj("新增失败!", "-1", false);
            }
        }
        return new ResultObj("新增成功", "1", true);
    }
    /**
     * 根据id修改农气信息 没有农气缩略图
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/updateAl")
    @ResponseBody
    public Object updateAl(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Addwendang wendang = jsonObject.getObject("addwendang", Addwendang.class);
                wendang.setPublisher(user.getUsername());
                wendang.setUploadtime(DateTime.now().toDate());
                Addwendang wdang = farmInfoService.updateArcticlelist(wendang,user);
                return new ResultObj("更新成功!", wdang, true);
            }
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
        return new ResultObj("更新失败!", "", false);
    }
    /**
     * 修改农气
     * @param params
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/updateArcticlelist")
    @ResponseBody
    public Object updateArcticlelist(@RequestParam("params") String params, @RequestParam("file") MultipartFile file,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                Addwendang wendang = jsonObject.getObject("addwendang", Addwendang.class);
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\ArcticleList\\";
                }else {
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/ArcticleList/";
                }
                FileUtil.mkDirs(urlpath);
                FileUtil.ClearTempFiles(urlpath, 100);
                String fileName = file.getOriginalFilename();// 文件名
                String fName = DateTime.now().toDateString() + fileName;
                String localPath = urlpath + fName;
                wendang.setPublisher(user.getUsername());
                wendang.setUploadtime(DateTime.now().toDate());
                wendang.setPicpath(fName);
                File localFile = new File(localPath);
                if (wendang == null)
                    return new ResultObj("更新失败!", "wendang传递有误!", false);
                //修改pg库
                Addwendang wdang = farmInfoService.updateArcticlelist(wendang,user);
                int returnTag = -1;
                if (localFile.exists()) {
                    returnTag = -1;
                }
                //修改mg库
                if (wdang != null) {
                    byte[] bytes = file.getBytes();
                    String bytesString = new String(bytes, "ISO-8859-1");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", wdang.getId());
                    map.put("table_name", "addwendang");
                    map.put("collectionName", "image_file");
                    map.put("advCode", DbConfig.advCode);
                    Map<String, Object> mapdata = new HashMap<String, Object>();
                    mapdata.put("data", bytesString.getBytes("ISO-8859-1"));
                    returnTag = (int) ms.updateData(map, mapdata);
                    file.transferTo(localFile);
                    if (returnTag != -1) {
                        return new ResultObj("更新成功", returnTag, true);
                    }
                }
            }
            return new ResultObj("更新失败!", "", false);
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
    }

    /**
     * 删除农气信息
     * @param request
     * @return
     */
    @RequestMapping("/deleteArcticlelist")
    @ResponseBody
    public Object deleteArcticlelist(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                String id = jsonObject.getString("id");
                users user = jsonObject.getObject("user", users.class);
                //删除pg
                Object wdang = farmInfoService.deleteArcticlelist(id, user);
                //删除mg
                if (wdang != null) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", id);
                    map.put("table_name", "addwendang");
                    map.put("collectionName", "image_file");
                    map.put("advCode", DbConfig.advCode);
                    int returnTag = (int) ms.deleteData(map);
                    if (returnTag != -1) {
                        return new ResultObj("删除成功", returnTag, true);
                    }
                }
            }
        } catch (Exception e) {
            return new ResultObj("删除失败", "", false);
        }
        return new ResultObj("删除失败", "", false);
    }
    /**
     * @Description: 关键字搜索信息公告
     **/
    @RequestMapping("/getMessageInfo")
    @ResponseBody
    public Object getMessageInfo(HttpServletRequest request) throws ParseException {
        String title = request.getParameter("title");
        Object message = farmInfoService.getMessage(title);
        ResultObj resultObj = new ResultObj("查询成功", message, true);
        return resultObj;
    }

    /**
     * @Description: 添加公告信息
     **/
    @RequestMapping("/insertMessages")
    @ResponseBody
    public Object insertMessages(@RequestParam("params") String params ,HttpServletRequest request) throws ParseException {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                String title = jsonObject.getString("title");
                String message = jsonObject.getString("message");
                MessageYuan messageYuan = new MessageYuan();
                messageYuan.setTitle(title);
                messageYuan.setMessage(message);
                messageYuan.setPeople(user.getUsername());
                messageYuan.setTime(DateTime.now().toDate());
                messageYuan.setOwner(DbConfig.CITY);
                Object result = farmInfoService.insertMessages(messageYuan, user);
                return result;
            }
            return new ResultObj("新增失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }

    /**
     *更新信息公告
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/updateMessages")
    @ResponseBody
    public Object updateMessages(@RequestParam("params") String params, HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                MessageYuan messageYuan = jsonObject.getObject("messageYuan", MessageYuan.class);
                messageYuan.setTime(DateTime.now().toDate());
                messageYuan.setPeople(user.getUsername());
                Object result = farmInfoService.updateMessages(messageYuan, user);
                return result;
            }
            return new ResultObj("更新失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("更新失败!", ex.getMessage(), false);
        }
    }
    /**
     * 删除公告
     * @param request
     * @return
     */
    @RequestMapping("/deleteMessages")
    @ResponseBody
    public Object deleteMessages(@RequestParam("params") String params,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                String id = jsonObject.getString("id");
                Object messages = farmInfoService.deleteMessages(id,user);
                return messages;
            }
        }catch (Exception ex) {
            return new ResultObj("删除失败!", ex.getMessage(), false);
        }
        return new ResultObj("删除失败!", "", false);
    }
    /**
     * @Description: 新增上报信息
     **/
    @RequestMapping("/insertShangBao")
    @ResponseBody
    public Object insertShangBao(@RequestParam("params") String params ,@RequestParam("file") MultipartFile file,HttpServletRequest request) throws ParseException {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                InfocollectorInfo infocollectorInfo = jsonObject.getObject("infocollectorinfo", InfocollectorInfo.class);
                if (infocollectorInfo == null)
                    return new ResultObj("新增失败!", "infocollectorInfo传递有误!", false);
                String urlpath ;
                if (operatingSystem.equals("Windows")){
                    urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\ShangBao\\";
                }else {
               urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/ShangBao/";
                }

                FileUtil.mkDirs(urlpath);
                FileUtil.ClearTempFiles(urlpath, 100);
                String fileName = file.getOriginalFilename();// 文件名
                String fName = DateTime.now().toDateString() + fileName;
                String localPath = urlpath + fName;
                infocollectorInfo.setImgurl(fName);
                File localFile = new File(localPath);
                //添加路径

                int returnTag = -1;
                if (localFile.exists()) {
                    returnTag = -1;
                }
                //新增pg库
                InfocollectorInfo info = farmInfoService.insertShangBao(infocollectorInfo, user);
                //新增mg库
                if (info != null) {
                    byte[] bytes = file.getBytes();
                    String bytesString = new String(bytes, "ISO-8859-1");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", info.getId());
                    map.put("table_name", "infocollector_info");
                    map.put("collectionName", "image_file");
                    map.put("advCode", DbConfig.advCode);
                    map.put("data", bytesString.getBytes("ISO-8859-1"));
                    returnTag = ms.insertData(map);
                    file.transferTo(localFile);
                    if (returnTag != -1) {
                        return new ResultObj("新增成功", returnTag, true);
                    }
                }
            }
            return new ResultObj("新增失败!", "", false);
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
    }

    @RequestMapping("/getInfoUser")
    @ResponseBody
    public Object getInfoUser(HttpServletRequest request) {
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        Map<String, String> selectParam = new HashMap<>();
        List<Infocollector> messages = new ArrayList<>();
        String[] columns = new String[]{"distinct infocollectorname"};
        selectRequest.setColumns(columns);
        try {
            selectParam.put("method", "infocollector");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = pc.processRequest(selectParam);
            messages = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<Infocollector>>() {
            });
        } catch (Exception ex) {
            return new ResultObj("查询失败!", ex.getMessage(), false);
        }
        return new ResultObj("查询成功!", messages, true);
    }

    /**
     * @Description: 网络上报查看
     **/
    @RequestMapping("/getShangBao")
    @ResponseBody
    public Object getShangBao(@RequestParam("params") String params, HttpServletRequest request) throws ParseException {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                String startDate = jsonObject.getString("startDate");
                String endDate = jsonObject.getString("endDate");
                String type = jsonObject.getString("type");
                String strname = jsonObject.getString("strname");
                //根据信息员名字查看它是否是
                List<InfocollectorInfo> shangBao = farmInfoService.getShangBao(strname,startDate,endDate, type);

                for (InfocollectorInfo info : shangBao) {
                    String serviceFilePath = "";
                    // 下载
                    String imgurl = info.getImgurl();
                    String path = request.getContextPath();
                    String basePath = DbConfig.IP + path
                            + "/Temp/ShangBao/";
                    String serviceUrl = basePath + imgurl;
                    String urlpath ;
                    if (operatingSystem.equals("Windows")){
                        urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\ShangBao\\";
                    }else {
                        urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/ShangBao/";
                    }
                    serviceFilePath = urlpath + imgurl;// 服务器文件
                    FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
                    FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存
                    // 如果目录中不存在文件，下载雷达文件到temp目录
                    File serviceFile = new File(serviceFilePath);// 服务器文件
                    if (!serviceFile.exists()) {// 把文件上传到服务器
                        Map<String, Object> queryMap = new HashMap<String, Object>();
                        queryMap.put("id", info.getId());
                        queryMap.put("table_name", "infocollector_info");
                        queryMap.put("collectionName", "image_file");
                        queryMap.put("advCode", DbConfig.advCode);
                        byte[] data = ms.selectData(queryMap);
                        if (data == null) {
                            serviceFilePath = "";
                        } else {
                            FileUtil.bytesToFile(data, serviceFilePath);
                        }
                    }
                    if (!serviceFilePath.equals("")) {
                        serviceFilePath = serviceUrl;
                    }

                    String imagepath = serviceFilePath;
                    //添加视频路径
                    info.setImgurl(imagepath);
                }
                return new ResultObj("查询成功!",shangBao , true);
            }
        } catch (Exception ex) {
            return new ResultObj("查询失败!", ex.getMessage(), false);
        }
        return new ResultObj("查询成功!","" , true);
    }


    /**
     * @Description: 网络上报统计
     **/
    @RequestMapping("/getShangBaoTotal")
    @ResponseBody
    public Object getShangBaoTotal(@RequestParam("params") String params,HttpServletRequest request) throws ParseException {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                String startDate = jsonObject.getString("startDate");
                String endDate = jsonObject.getString("endDate");
                String type = jsonObject.getString("type");
                String strname = jsonObject.getString("strname");
                //根据信息员名字查看它是否是

                List<InfocollectorInfo> shangBao = farmInfoService.getShangBaoTotal(strname,startDate,endDate, type);
                return new ResultObj("查询成功!",shangBao , true);
            }
        } catch (Exception ex) {
            return new ResultObj("查询失败!", ex.getMessage(), false);
        }
        return new ResultObj("查询成功!","" , true);
    }

    /**
     * 灾情统计
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/inertZhShangBao")
    @ResponseBody
    public Object inertZhShangBao(@RequestParam("params") String params,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                EmergencyDisaster disaster = jsonObject.getObject("EmergencyDisaster", EmergencyDisaster.class);
                if (disaster == null)
                    return new ResultObj("新增失败!", "disaster传递有误!", false);
                Object result = farmInfoService.inertZhShangBao(disaster, user);
                return result;
            }
        } catch (Exception ex) {
            return new ResultObj("新增失败!", ex.getMessage(), false);
        }
        return new ResultObj("新增失败!", "params不能正确解析", false);
    }
    /**
     * 下发信息
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/insertManageInfo")
    @ResponseBody
    public Object insertManageInfo(@RequestParam("params") String params,HttpServletRequest request) {
        try {
            if (!StringUtil.IsNullOrEmpty(params)) {
                JSONObject jsonObject = JSONObject.parseObject(params);
                users user = jsonObject.getObject("user", users.class);
                ManageInfo manageInfo = jsonObject.getObject("ManageInfo", ManageInfo.class);
                if (manageInfo == null)
                    return new ResultObj("下发失败!", "disaster传递有误!", false);
                Object result = farmInfoService.insertManageInfo(manageInfo, user);
                return result;
            }
            return new ResultObj("下发失败!", "params不能正确解析", false);
        } catch (Exception ex) {
            return new ResultObj("下发失败!", ex.getMessage(), false);
        }
    }


    /**
     * @Description: 查询信息InfoCollector
     **/
    @RequestMapping("/getInfoCollector")
    @ResponseBody
    public Object getInfoCollector(HttpServletRequest request) throws ParseException {
        Object message = farmInfoService.getInfoCollector();
        ResultObj resultObj = new ResultObj("查询成功", message, true);
        return resultObj;
    }

    /**
     * @Description: 查询站点信息
     **/
    @RequestMapping("/getStationInfo")
    @ResponseBody
    public Object getStationInfo (HttpServletRequest request) throws ParseException {
        String stationid = request.getParameter("stationid");
        Object message = farmInfoService.getStationInfo(stationid);
        ResultObj resultObj = new ResultObj("查询成功", message, true);
        return resultObj;
    }

    /**
     * @Description: 下信息下发查看
     **/
    @RequestMapping("/getManageInfo")
    @ResponseBody
    public Object getManageInfo(HttpServletRequest request) throws ParseException {
        String username = request.getParameter("username");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        Object message = farmInfoService.getManageInfo(username,startDate,endDate);
        ResultObj resultObj = new ResultObj("查询成功", message, true);
        return resultObj;
    }

    /**
     * @Description: 下发信息统计
     **/
    @RequestMapping("/getXiaLaTongji")
    @ResponseBody
    public Object getXiaLaTongji(HttpServletRequest request) throws ParseException {
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        Object message = farmInfoService.getXiaFaTongji(startDate,endDate);
        ResultObj resultObj = new ResultObj("查询成功", message, true);
        return resultObj;
    }
    /**
     * @Description: 获取县市列表
     * @Author: sunshine
     * @Date: 2021/7/29 13:52
     **/
    @RequestMapping("/getCountyCity")
    @ResponseBody
    public Object getCountyCity() {
        List<county_point> county_points = farmInfoService.getCountyCity();
        ResultObj resultObj = new ResultObj("查询成功", county_points, true);
        return resultObj;
    }

    /**
     * 获取市级
     * @return
     */
    @RequestMapping("/getCity")
    @ResponseBody
    public Object getCity() {
        List<admin_code> county_points = farmInfoService.getCity();
        ResultObj resultObj = new ResultObj("查询成功", county_points, true);
        return resultObj;
    }
}
